package src.servletController;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import src.servletService.ServiceFactory;
import src.servletUtils.MyUtils;
import src.servletbean.UsersBean;

public class AddStudent extends HttpServlet {
	private static final long serialVersionUID = 1L;
	
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// 1、获取表单数据		
		// 2、将保单数据填充到实体类
		//修改编码
//		request.setCharacterEncoding("utf-8");//使用了过滤器，用filter已经进行编码，进行配置文件修改就可以
		
		UsersBean usersB = (UsersBean) MyUtils.getNewInstance(UsersBean.class);
		//获取id
		usersB.setUserid(MyUtils.obj2Integer(request.getParameter("txtUserId")));
		usersB.setUsername(request.getParameter("txtUserName"));
		usersB.setUserage(MyUtils.obj2Int(request.getParameter("txtUserAge")));
		usersB.setUseraddress(request.getParameter("txtUserAddress"));
		usersB.setUserpsw(request.getParameter("txtUserPsw"));
		usersB.setUsersex(request.getParameter("selectSext"));
		usersB.setUsertel(request.getParameter("txtUserTel"));
		// 3、转发Model层进行业务逻辑处理
		int i =ServiceFactory.getUserInfoManagerServiceInstance().saveUserInfo(usersB);
		
		// 4、根据处理结果进行view层转发，将结果显示给用户客户端
		if(i>0){
			//数据保存成功
			//准备一下在添加成功的页面上进行数据显示
			//获取需要显示的所有的数据集合
			List<UsersBean> lstUsers = ServiceFactory.getUserInfoManagerServiceInstance().getAllUsersInfo();
			//将需要显示的结果集放入session缓存中
			request.getSession().setAttribute("lstUsers", lstUsers);
			//清空request缓存中的对象
			request.removeAttribute("usersB");
			//转发到结果显示页面
			request.getRequestDispatcher("logined/loginStudent/loginStudentDetail.jsp").forward(request, response);
		}else{
			//显示错误信息提示页面
			request.getSession().setAttribute("pageErrorInfo", "用户信息添加失败~~~");
			response.sendRedirect("logined/error.jsp");
		}
	}	
	
	
}
